package org.proyectsource.pslogger;

import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.Date;

/**
 * @author Aitor González Fernández
 * @version Revisión $Id:$
 */

public class Logger {
    private static Boolean toFile = false;
    private static Boolean toConsole = false;
    private static Boolean toGUI = false;

    public enum MessageType {DEBUG, WARNING, INFO, ERROR, FATAL};

    private static Boolean toDebug = false;

    private static FileWriter file = null;
    private static PrintWriter pw = null;

    static {
        Init(true, false, false);
    }
    
    public static void Init(Boolean file, Boolean console, Boolean gui) {
        toFile = file;
        toConsole = console;
        toGUI = gui;
        
        if (toFile) {
            initializeFile();
        }
        else {
            uninitializeFile();
        }
    }
    
    public static void initializeFile() {
        try {
            file = new FileWriter("./log.txt", true);
            pw = new PrintWriter(file);
        }
        catch (Exception e) {
        }
    }
    
    public static void uninitializeFile() {
        try {
            pw.close();
            file.close();
        }
        catch (Exception e) {
        }
    }
    
    public static void setDebug(Boolean active) {
        toDebug = active;
    }
    
    public static Boolean isDebug() {
        return toDebug;
    }

    public static void SendMessage (MessageType msgt, String msg) {
        if (!toDebug && msgt == MessageType.DEBUG) {
            return;
        }

        if (toFile) {
            SendMessageToFile(msgt, msg);
        }
        if (toConsole) {
            System.err.println(new Date() + " -> " + msgt.name() + " -> " + msg);
        }
        if (toGUI) {
            // TODO GUI System
        }
    }

    public static void SendMessageToFile (MessageType msgt, String msg) {
        pw.println(new Date() + " -> " + msgt.name() + " -> " + msg);
        pw.flush();
    }
}
